<template>
  <el-dialog :title="title" :visible.sync="open" width="400px">
    <el-form
      ref="formData"
      :model="formData"
      :rules="rules"
      size="small"
      label-width="80px"
    >
      <el-form-item prop="roleName" label="角色名称">
        <el-input v-model="formData.roleName" placeholder="角色名称"></el-input>
      </el-form-item>
      <el-form-item prop="desc" label="角色描述">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入内容"
          v-model="formData.desc"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        @click="onSubmit('formData')"
        size="small"
        :loading="loading"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>
<script>
import { addAndUpdateRole } from "@/api/userManagement/roles";
export default {
  data() {
    return {
      open: false,
      loading: false,
      title: "",
      formData: {
        roleName: "",
        desc: "",
      },
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    show(row) {
      console.log(row);
      this.open = true;
      if (row) {
        this.title = "编辑角色";
        this.formData = {
          roleName: row.roleName,
          desc: row.desc,
          id: row.id,
        };
      } else {
        this.formData = {
          roleName: "",
          desc: "",
        };
        this.title = "新增角色";
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          addAndUpdateRole(this.formData).then((res) => {
            this.loading = false;
            if (res.data.resultStatus) {
              this.open = false;
              this.$message.success("新增成功");
              this.$emit("load");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>